package classics.sort;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Description
 * @Author asrieal
 * @Date 2021/9/13
 */
public class topk {
    public static void main(String[] args) {
        int[] num = {1,55,33,66,36,875,4,6,32};
        int k = 3;
        //默认是小根堆，也就是升序排列
        PriorityQueue<Integer> queue = new PriorityQueue<>(k, (a,b)->a-b);

        for (int i = 0; i < k; i++) {
            queue.add(num[i]);
        }
        for (int i = k; i < num.length; i++) {
            int temp = queue.peek();
            if(num[i]>temp){
                queue.poll();   //需要弹出堆头元素，不然就扩容了。
                queue.add(num[i]);
            }
        }

        while(!queue.isEmpty()){
            System.out.println(queue.poll());
        }

    }
}
